package com.blog.admin.controller;

import com.blog.admin.service.CategoryService;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.domain.dto.CategoryDto;
import com.blog.common.domain.dto.ConditionDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 分类后台控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/3/12
 **/
@Api("分类后台管理")
@RestController
public class CategoryBackController {

    @Resource
    private CategoryService categoryService;

    /**
     * 根据条件搜索分类
     * @param condition 条件
     * @return AjaxResult
     */
    @ApiOperation(value = "根据条件搜索分类")
    @GetMapping("/categories/search")
    public AjaxResult listCategoriesBySearch(ConditionDto condition){
        return AjaxResult.success(categoryService.listCategoriesBySearch(condition));
    }

    /**
     * 查询分类
     * @param condition 条件
     * @return AjaxResult
     */
    @ApiOperation(value = "查询分类")
    @GetMapping("/categories/list")
    public AjaxResult listCategories(ConditionDto condition){
        return AjaxResult.success(categoryService.listCategories(condition));
    }

    /**
     * 根据id查询分类
     * @param categoryId 分类id
     * @return AjaxResult
     */
    @ApiOperation(value = "根据id查询分类")
    @GetMapping("/categories/{categoryId}")
    public AjaxResult getCategoriesById(@PathVariable("categoryId") Serializable categoryId){
        return AjaxResult.success(categoryService.getCategoriesById(categoryId));
    }

    /**
     * 新增分类
     *
     * @param categoryDto 分类dto
     * @return {@link AjaxResult}
     */
    @ApiOperation(value = "新增分类")
    @PostMapping("/categories")
    public AjaxResult saveCategories(@RequestBody CategoryDto categoryDto){
        return AjaxResult.success(categoryService.saveCategories(categoryDto));
    }

    /**
     * 删除分类
     *
     * @param categoryIds 分类id
     * @return {@link AjaxResult}
     */
    @ApiOperation(value = "删除分类")
    @DeleteMapping("/categories/{categoryIds}")
    public AjaxResult deleteCategories(@PathVariable("categoryIds") Serializable categoryIds){
        Boolean flag = categoryService.deleteCategories(categoryIds);
        return flag ? AjaxResult.success() : AjaxResult.error("无法删除默认分类");
    }

    /**
     * 删除分类
     *
     * @param categoryDto 分类
     * @return {@link AjaxResult}
     */
    @ApiOperation(value = "修改分类")
    @PutMapping("/categories")
    public AjaxResult updateCategories(@RequestBody CategoryDto categoryDto){
        return AjaxResult.success(categoryService.updateCategories(categoryDto));
    }
}
